/**
 * 
 */
package neptune.web.client.view.components.editor;

import neptune.web.client.model.topology.SerializableNetworkInterface;
import neptune.web.client.model.topology.SerializableNode;
import neptune.web.client.model.topology.SerializableTopology;
import neptune.web.client.utils.TopologyGenerationUtils;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.TreeItem;

/**
 * @author Roberto Bifulco [RobertoBifulco.it] [info@robertoBifulco.it]
 * 
 */
public class AddNetIFCommand implements Command {

	private TreeItem item;
	private SerializableNode node;
	private final SerializableTopology topology;

	public AddNetIFCommand(SerializableNode node,
			SerializableTopology topology, TreeItem item) {
		this.node = node;
		this.topology = topology;
		this.item = item;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.gwt.user.client.Command#execute()
	 */
	public void execute() {
		final SerializableNetworkInterface netIF = new SerializableNetworkInterface();
		netIF.setInterfaceName(TopologyGenerationUtils
				.defineNewInterfaceNameForNode(node));

		node.getInterfaces().add(netIF);

		final TreeItem netIfItem = new TreeItem();
		netIfItem.setWidget(new NetworkIFMenuBar(netIF, node, topology,
				netIfItem));
		
		if (item.getText().equals("Interfaces"))
			item.addItem(netIfItem);
		else
			item.getChild(0).addItem(netIfItem);

		NetIFPropsDialog dialog = new NetIFPropsDialog(netIF, node, topology,
				netIfItem);
		dialog.setCancelCallback(new CancelCallBack() {
			public void onCancel() {
				netIfItem.remove();
				node.getInterfaces().remove(netIF);
			}
		});
		dialog.center();
	}

}
